﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Linq.Expressions;
using EntityFrameworks.Entity.Core.Specification;
using IRepository.Core;
using EntityFrameworks.Entity.Core;
namespace EntityFrameworks.Data.Core
{
    /// <summary>
    /// 模拟测试数据上下文，它的生命周期为一个调试上下文的结束
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class MockRepository<TEntity> :
         IExtensionRepository<TEntity> where TEntity : class
    {
        private readonly Dictionary<Type, IList> _entities = new Dictionary<Type, IList>();

        public List<TEntity> GetTable<TEntity>()
        {
            if (!this._entities.ContainsKey(typeof(TEntity)))
            {
                this._entities[typeof(TEntity)] = new List<TEntity>();
            }
            return (List<TEntity>)this._entities[typeof(TEntity)];
        }

        #region IExtensionRepository<TEntity> 成员

        public void Insert(IEnumerable<TEntity> item)
        {
            item.ToList().ForEach(this.Insert);
        }

        public void Update(IEnumerable<TEntity> item)
        {
            item.ToList().ForEach(this.Update);
        }

        public void Delete(IEnumerable<TEntity> item)
        {
            item.ToList().ForEach(this.Delete);
        }

        public void Update<T>(Expression<Action<T>> entity) where T : class
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel(ISpecification<TEntity> specification)
        {
            return this.GetModel().Where(specification.SatisfiedBy());
        }

        public IQueryable<TEntity> GetModel(Expression<Func<TEntity, bool>> predicate)
        {
            return this.GetModel().Where(predicate);
        }

        public TEntity Find(Expression<Func<TEntity, bool>> predicate)
        {
            return this.GetModel().FirstOrDefault(predicate);
        }

        public TEntity Find(ISpecification<TEntity> specification)
        {
            return this.GetModel().FirstOrDefault(specification.SatisfiedBy());
        }

        public void BulkInsert(IEnumerable<TEntity> item, bool isRemoveIdentity)
        {
            throw new NotImplementedException();
        }

        public void BulkInsert(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public void BulkUpdate(IEnumerable<TEntity> item, params string[] fieldParams)
        {
            throw new NotImplementedException();
        }

        public void BulkDelete(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public event Action<SavedEventArgs> AfterSaved;

        public event Action<SavedEventArgs> BeforeSaved;

        #endregion

        #region IRepository<TEntity> 成员

        public void Insert(TEntity item)
        {
            this.GetTable<TEntity>().Add(item);
        }

        public void Delete(TEntity item)
        {
            this.GetTable<TEntity>().Remove(item);
        }

        public void Update(TEntity item)
        {
            List<TEntity> entities = this.GetTable<TEntity>();
            int index = entities.IndexOf(item);
            if (index > -1)
            {
                entities[index] = item;
            }
        }

        public IQueryable<TEntity> GetModel()
        {
            return this.GetTable<TEntity>().AsQueryable();
        }

        public TEntity Find(params object[] id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region MyRegion Orderable Member
        public IQueryable<TEntity> GetModel(Action<IOrderable<TEntity>> orderBy)
        {
            var linq = new Orderable<TEntity>(GetModel());
            orderBy(linq);
            return linq.Queryable;
        }

        public IQueryable<TEntity> GetModel(Action<IOrderable<TEntity>> orderBy, ISpecification<TEntity> specification)
        {
            var linq = new Orderable<TEntity>(GetModel(specification));
            orderBy(linq);
            return linq.Queryable;
        }

        public IQueryable<TEntity> GetModel(Action<IOrderable<TEntity>> orderBy, Expression<Func<TEntity, bool>> predicate)
        {
            var linq = new Orderable<TEntity>(GetModel(predicate));
            orderBy(linq);
            return linq.Queryable;
        }
        #endregion


        public IQueryable<TEntity> GetEntities(ISpecification<TEntity> specification)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetEntities()
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetEntities(Expression<Func<TEntity, bool>> predicate)
        {
            throw new NotImplementedException();
        }
    }
}
